/*
*	Copyright (C) 2008-2009  Devioux.com
*
*    This program is free software: you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package com.devioux.inter;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import com.devioux.inter.str.StrParser;

public class VarRunner {
	
	private List<String> content;
	private List<String> str;
	
	public VarRunner(List<String> content) throws NumberFormatException, IllegalArgumentException, SecurityException, IllegalAccessException, InvocationTargetException, ClassNotFoundException, InstantiationException, NoSuchMethodException {
		this.content = content;
		str          = new ArrayList<String>();
		run();
		
		if (str.size() != 0)
			strRun();
	}
	
	private void run() throws NumberFormatException, IllegalArgumentException, SecurityException, IllegalAccessException, InvocationTargetException, ClassNotFoundException, InstantiationException, NoSuchMethodException {
		
		for (int i = 0; i < content.size(); i++) {
			if (content.get(i).startsWith("int") && new VarChecker(content.get(i).substring(content.get(i).indexOf("int") + 3, content.get(i).indexOf("=")).trim()).checkExsist() == false) {
				
				com.devioux.inter.lang.Integer.add(content.get(i).substring(content.get(i).indexOf("int") + 3, content.get(i).indexOf("=")).trim(), Integer.parseInt(content.get(i).substring(content.get(i).indexOf("=") + 2, content.get(i).indexOf(";")).trim()));
			
			} else if (content.get(i).startsWith("boolean") && new VarChecker(content.get(i).substring(content.get(i).indexOf("boolean") + 7, content.get(i).indexOf("=")).trim()).checkExsist() == false) {
				
				com.devioux.inter.lang.Boolean.add(content.get(i).substring(content.get(i).indexOf("boolean") + 7, content.get(i).indexOf("=")).trim(), Boolean.parseBoolean(content.get(i).substring(content.get(i).indexOf("=") + 2, content.get(i).indexOf(";")).trim()));
			
			} else if (content.get(i).startsWith("String") && new VarChecker(content.get(i).substring(content.get(i).indexOf("String") + 6, content.get(i).indexOf("=")).trim()).checkExsist() == false) {
				
				str.add(content.get(i));
			
			} else {
				//Throw variable already exists exception
			}
		}
	}
	
	private void strRun() throws IllegalArgumentException, SecurityException, IllegalAccessException, InvocationTargetException, ClassNotFoundException, InstantiationException, NoSuchMethodException {
		for (int i = 0; i < str.size(); i++) {
			com.devioux.inter.lang.String.add(content.get(i).substring(content.get(i).indexOf("String") + 6, content.get(i).indexOf("=")).trim(), new StrParser(content.get(i).substring(content.get(i).indexOf("=") + 2, content.get(i).indexOf(";")).trim()).getString());
		}
	}
	
	
}
